package jsu.xxy.coffee3.Utils;

import io.jsonwebtoken.*;

import java.util.Date;
import java.util.UUID;

public class Test {

    private  long time = 1000*60*24*60;
    private String signature = "admin";
    public  void jwt(){
        JwtBuilder jwtBuilder = Jwts.builder();
        String jwtToken = jwtBuilder
//                head
                .setHeaderParam("typ","JWT")
                .setHeaderParam("alg","HS256")
//                payload
                .claim("username","tom")
                .claim("role","admin")
                .setSubject("test")//主题
                .setExpiration(new Date(System.currentTimeMillis()+time))//有效时间
                .setId(UUID.randomUUID().toString())
//                signature
                .signWith(SignatureAlgorithm.HS256,signature)
                .compact();
        System.out.println(jwtToken);
    }

    public void parse(){
        String token="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InRvbSIsInJvbGUiOiJhZG1pbiIsInN1YiI6InRlc3QiLCJleHAiOjE2ODYxNTUwMDEsImp0aSI6IjFjNWMwNzUwLTc3MmQtNGE3Ni05ZTZiLWE4NzE1NWZiZDZmYSJ9.FBWx1ufHSfO5EyY5WsN4V9yFUIEKj78U4ANVcLk2ZdM";
        JwtParser jwtParser = Jwts.parser();
        Jws<Claims> claimsJws = jwtParser.setSigningKey(signature).parseClaimsJws(token);
        Claims claims = claimsJws.getBody();
        System.out.println(claims.get("username"));
        System.out.println(claims.get("role"));
        System.out.println(claims.getId());
        System.out.println(claims.getExpiration());
    }
    public static void main(String[] args) {
        new Test().parse();
    }
}
